package com.lagou.service;

import com.alibaba.fastjson.JSON;
import com.lagou.entity.CustomUser;
import com.lagou.mapper.CustomUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class MeetService {

    @Autowired
    private CustomUserMapper customUserMapper;

    @Qualifier(value = "redis")
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public CustomUser queryUser(CustomUser record) {
        String cache = redisTemplate.opsForValue().get("customUser");
        CustomUser customUser = null;
        if (!StringUtils.isBlank(cache)) {
            return JSON.parseObject(cache, CustomUser.class);
        } else {
            customUser = customUserMapper.selectByPrimaryKey(record.getId());
            redisTemplate.opsForValue().set("customUser", JSON.toJSONString(customUser));
        }
        return customUser;
    }

    /**
     * queryByPage
     *
     * @param pageCurr 当前页
     * @param pageSize 当前页大小
     * @return List<CustomUser>
     */
    public List<CustomUser> queryByPage(int pageCurr, int pageSize) {
        if (pageCurr <= 0) {
            pageCurr = 1;
        }
        if (pageSize <= 0) {
            pageSize = 1;
        }
        // 查询起始行索引值
        pageCurr = (pageCurr - 1) * pageSize;
        List<CustomUser> customUsers = customUserMapper.selectByPage(pageCurr, pageSize);
        return customUsers;
    }
}
